Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
How to customize formatting of code that is generated by "Encapsulate Field"?

Previously I am fairly certain that the "Encapsulate Field" command would turn something like the following:

public int SomeNumber;

into the following (what I want from VS 2015):

private int someNumber;

public int SomeNumber {
    get { return someNumber; }
    set { someNumber = value; }
}

but in Visual Studio 2015 I am seeing the following:

private int someNumber;

public int SomeNumber {
    get {
        return someNumber;
    }

    set {
        someNumber = value;
    }
}

Is there a way to fix this?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This was a design change in VS2015. In previous versions, the refactoring command paid attention to the Tools > Options > Text Editor > C# > Wrapping > "Leave block on single line" option. With it turned on, you'll get the property getter and setter body the way it encoded in the snippet, braces on the same line. The way you like it.

Different in VS2015, it now pays attention to the Tools > Options > Text Editor > C# > Formatting > New Lines > "Place open brace on new line for methods" setting. You get to choose between "egyptian" braces or having the opening brace separate. Neither of which you like.

Accidents happen when Microsoft creates new VS versions, this was not an accident. Whether this was done by "popular demand" is hard to reverse-engineer, I consider it pretty likely since this refactoring is usually done to write a non-trivial getter or setter, the kind that won't fit a single line. Providing us with a choice between all three possible formatting preferences looks like a problem to me, the existing formatting options are not a good match.

Only real option is to let Microsoft know that you are not happy with the change. There is an existing UserVoice article that proposes a change. You can vote for it or write your own. Post a link to it in your question so other SO users can vote.

over 4 years ago · Santiago Trujillo Report

0

Will that help you:
-Encapsulate Field Refactoring (C#): https://msdn.microsoft.com/en-us/library/a5adyhe9.aspx

This website seems to offer a solution a the end of the topic.

Check also this post : -Different Refactoring style for field encapsulation: how to make Visual Studio change it? Different Refactoring style for field encapsulation: how to make Visual Studio change it?

This one is corresponding to your question in a certain manner: the question is really related to yours :)

over 4 years ago · Santiago Trujillo Report

0

I myself have tried changing the snippet file to suite my file but VS doesn't take effect. What I end up doing is

  1. Encapsulate the fields as usual.
  2. Copy and paste the code into notepad++ and do a Find and Replace.

Find:

(\{)*(\s*)*(get|set)\r\n\s+{\r\n\s+(.*)\r\n\s+\}\s+

Replace with:

$1$3\{$4\}
  1. Paste the result back into VS. VS will format it follow the "Leave block on single line".
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!